marvelfandomcom_pt_br-20200213-history
M
local InfoboxBuilder = {} -- Define dependecies local HF = require("Module:HF") local View = require("Module:InfoboxBuilderView") -- Define var for custom user's module local CM = {} -- Define default variables InfoboxBuilder.vars = { Theme = "default", -- Adds prefix to CSS classes CustomModule = " ", -- Defines a path to a module with custom functions MainImageCaption = "Off", -- Toggles display of Label in the Main Image field ToggleContentLongerThan = 1000 -- Makes fields with long values collapsible } function InfoboxBuilder.builder( frame ) local input = InfoboxBuilder.parse( frame.args ) local Infobox = View.render( input, InfoboxBuilder.vars ) return Infobox end function InfoboxBuilder.parse( args ) local input = {} input.fields = {} -- Actual Infobox data input.sections = {} -- Relating headers to lines input.vars = {} -- Variables useful for styling etc. -- Distribute args to vars and fields. local indexes = {} -- Table used to sort indexes of fields local fields = {} -- Temporary fields table local f = 0 -- Fields consecutive indice for k, v in pairs(args) do local keySplit = HF.explode( ":", tostring(k) ) local index = tonumber( HF.trim( keySplit1 ) ) local key = tostring( HF.trim( keySplit2 ) ) local value = tostring( HF.trim( v ) ) if index > 0 then if fieldsindex nil then table.insert( indexes, index ) fieldsindex = {} end fieldsindexkey = value else if not HF.isempty( HF.trim( value ) ) then InfoboxBuilder.varskey = value end end end input.vars = InfoboxBuilder.vars table.sort( indexes ) local currentHeader = 0 for i, index in ipairs(indexes) do f = f + 1 input.fieldsf = {} input.fieldsf = fieldsindex -- Add fields to sections if input.fieldsf"Type" "Header" then currentHeader = f input.sectionsf = "Off" -- Hide header by default, show it when it has at least 1 line elseif input.fieldsf"Type" "Line" and currentHeader > 0 then -- Prevent false sections without a header if not HF.isempty( input.fieldsf"Value" ) then input.sectionscurrentHeader = "On" -- Show header if there is at least one line filled in its section end end if input.fieldsf"Type" "Split" then input.fieldsf"Label" = { input.fieldsf"LabelLeft", input.fieldsf"LabelRight" } input.fieldsf"Value" = { input.fieldsf"ValueLeft", input.fieldsf"ValueRight" } end end input = InfoboxBuilder.execute( input ) return input end function InfoboxBuilder.execute( input ) -- Require user's custom module if not HF.isempty( HF.trim( InfoboxBuilder.vars"CustomModule" ) ) then CM = require( HF.trim( InfoboxBuilder.vars"CustomModule" ) ) -- Execute custom methods for index, field in ipairs( input.fields ) do if type( field.Label ) "table" then if InfoboxBuilder.methodExists( field.LabelMethod ) then input.fieldsindex.Label = CM[ field.LabelMethod ]( field, InfoboxBuilder.vars ) end elseif not HF.isempty( HF.trim( field.Label ) ) and InfoboxBuilder.methodExists( field.LabelMethod ) then input.fieldsindex.Label = CM[ field.LabelMethod ]( field, InfoboxBuilder.vars ) end if type( field.Value ) "table" then if InfoboxBuilder.methodExists( field.ValueMethod ) then input.fieldsindex.Value = CM[ field.ValueMethod ]( field, InfoboxBuilder.vars ) end elseif not HF.isempty( HF.trim( field.Value ) ) and InfoboxBuilder.methodExists( field.ValueMethod ) then input.fieldsindex.Value = CM[ field.ValueMethod ]( field, InfoboxBuilder.vars ) end end end return input end function InfoboxBuilder.methodExists( name ) local exists = false if type( name ) ~= nil then if not HF.isempty( name ) then if type( CM[ name ] ) "function" then exists = true end end end return exists end return InfoboxBuilder